package com.hyx.api.service.impl.pc_admin;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.hyx.api.domain.VerifyModelOption;
import com.hyx.api.domain.bo.VerifyModelOptionBo;
import com.hyx.api.domain.vo.VerifyModelOptionVo;
import com.hyx.api.mapper.VerifyModelOptionMapper;
import com.hyx.api.service.pc_admin.IVerifyModelOptionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 验机选项Service业务层处理
 *
 * @author HK
 * @date 2023-10-21
 */
@RequiredArgsConstructor
@Service
public class VerifyModelOptionServiceImpl implements IVerifyModelOptionService {
    private final VerifyModelOptionMapper verifyModelOptionMapper;

    /**
     * 查询验机选项
     *
     * @param id 验机选项主键
     * @return 验机选项
     */
    @Override
    public VerifyModelOptionVo selectVerifyModelOptionById(Long id) {
        //TODO 表以外的字段，写入vo
        return verifyModelOptionMapper.selectVoById(id);
    }

    /**
     * 查询验机选项列表
     *
     * @param bo 验机选项
     * @return 验机选项
     */
    @Override
    public List<VerifyModelOptionVo> selectVerifyModelOptionList(VerifyModelOptionBo bo) {
        return verifyModelOptionMapper.queryModelOptionsList(bo);
    }

    /**
     * 新增验机选项
     *
     * @param bo 验机选项
     * @return 结果
     */
    @Override
    public int insertVerifyModelOption(VerifyModelOptionBo bo) {
        VerifyModelOption verifyModelOption = tranEntityBeforeSave(bo);
        verifyModelOption.setStatus(0);
        verifyModelOption.setDelFlag(0);
        validEntityBeforeSave(verifyModelOption);
        return verifyModelOptionMapper.insert(verifyModelOption);
    }

    /**
     * 修改验机选项
     *
     * @param bo 验机选项
     * @return 结果
     */
    @Override
    public int updateVerifyModelOption(VerifyModelOptionBo bo) {
        VerifyModelOption verifyModelOption = tranEntityBeforeSave(bo);
        return verifyModelOptionMapper.updateById(verifyModelOption);
    }

    /**
     * 批量删除验机选项
     *
     * @param ids 需要删除的验机选项主键
     * @return 结果
     */
    @Override
    public int deleteVerifyModelOptionByIds(Long[] ids) {
        List<VerifyModelOption> list = new ArrayList<>();
        for (Long id : ids) {
            VerifyModelOption verifyModelOption = new VerifyModelOption();
            verifyModelOption.setDelFlag(2);
            verifyModelOption.setId(id);
            list.add(verifyModelOption);
        }
        boolean bool = verifyModelOptionMapper.updateBatchById(list);
        return bool ? 1 : 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VerifyModelOption entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 保存前的数据转换
     */
    private VerifyModelOption tranEntityBeforeSave(VerifyModelOptionBo bo) {
        //TODO 做一些数据转换
        if (ObjectUtils.isEmpty(bo.getType()))
            bo.setType(1);  //规格
        if (ObjectUtils.isNotEmpty(bo.getParentId()) && bo.getParentId() > 0) {
            VerifyModelOptionVo vo = selectVerifyModelOptionById(bo.getParentId());
            bo.setType(vo.getType());
        }
        VerifyModelOption verifyModelOption = new VerifyModelOption();
        BeanUtils.copyProperties(bo, verifyModelOption);
        return verifyModelOption;
    }
}
